define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cate/ceshi/index' + location.search,
                    add_url: 'cate/ceshi/add',
                    edit_url: 'cate/ceshi/edit',
                    del_url: 'cate/ceshi/del',
                    multi_url: 'cate/ceshi/multi',
                    import_url: 'cate/ceshi/import',
                    table: 'cate_ceshi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                pagination: false,
                search: false,
                commonSearch: false,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), searchable: false},
                        {field: 'cate_name', title: __('Cate_name'), align: 'left', searchable: false, formatter: function(value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                        }},
                        {field: 'type', title: __('Type'), searchable: false,  formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        //searchList: {"0":__('Status 0'),"1":__('Status 1')},
                        {field: 'status', title: __('Status'), searchable: true, operate:false, formatter: Table.api.formatter.status,
                            searchList: {"0": __('Status 0'),"1": __('Status 1')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                let typeStr = $(this).attr("href").replace('#', '');
                let options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    params.status = typeStr;
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
